package cn.jinjimi.app.main.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import cn.jinjimi.base.util.DateUtils;
import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.app.R;
import cn.jinjimi.app.custom.entity.Custom;
import cn.jinjimi.app.custom.entity.Insurance;
import cn.jinjimi.app.custom.entity.RemindBean;
import cn.jinjimi.app.entity.Holiday;
import cn.jinjimi.app.main.adapter.extend.MyBaseAdapter;
import cn.jinjimi.app.main.dialog.DatePickerDialog;

/**
 * 提醒列表
 * <p/>
 * Created by jinsen on 15/11/20.
 */
public class CustomRemindAdapter extends MyBaseAdapter<RemindBean> {
    final int TYPE_COUNT = 2;
    final int TYPE_TITLE = 0;
    final int TYPE_CONTENT = 1;

    Context mContext;

    int mInsuranceColor;
    int mCustomerColor;
    int mHolidayColor;

    Drawable mLunarMarkIcon;

    public CustomRemindAdapter(Context context) {
        this.mContext = context;
        mCustomerColor = ContextCompat.getColor(mContext, R.color.common_color_1);
        mInsuranceColor = ContextCompat.getColor(mContext, R.color.common_color_2);
        mHolidayColor = ContextCompat.getColor(mContext, R.color.common_color_3);

        mLunarMarkIcon = ContextCompat.getDrawable(mContext, R.drawable.logo);
        mLunarMarkIcon.setBounds(0, 0, mContext.getResources().getDimensionPixelSize(R.dimen.dp_12), mContext.getResources().getDimensionPixelSize(R.dimen.dp_12));
    }

    @Override
    public int getViewTypeCount() {
        return TYPE_COUNT;
    }

    @Override
    public int getItemViewType(int position) {
        RemindBean bean = getItem(position);
        if (bean.type == RemindBean.TYPE_TITLE) {
            return TYPE_TITLE;
        } else
            return TYPE_CONTENT;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = null;
        TitleHoler titleHolder = null;
        int type = getItemViewType(position);
        if (convertView == null) {
            if (type == TYPE_CONTENT) {
                holder = new Holder();
                convertView = View.inflate(mContext, R.layout.item_remind_layout, null);
                holder.type = (TextView) convertView.findViewById(R.id.item_remind_type);
                holder.title = (TextView) convertView.findViewById(R.id.item_remind_title);
                holder.date = (TextView) convertView.findViewById(R.id.item_remind_date);
                holder.countDown = (TextView) convertView.findViewById(R.id.item_remind_countdown);
                holder.countDownUine = (TextView) convertView.findViewById(R.id.item_remind_countdown_unit);
                convertView.setTag(holder);
            } else {
                titleHolder = new TitleHoler();
                convertView = View.inflate(mContext, R.layout.item_remind_title_type_layout, null);
                titleHolder.title = (TextView) convertView.findViewById(R.id.item_remind_title_text);
                convertView.setTag(titleHolder);
            }
        } else {
            if (type == TYPE_CONTENT) {
                holder = (Holder) convertView.getTag();
            } else {
                titleHolder = (TitleHoler) convertView.getTag();
            }
        }

        RemindBean data = getItem(position);

        if (type == TYPE_CONTENT) {
            setContentItem(data, holder);
        } else {
            setTitleItem(data, titleHolder);
        }

        return convertView;
    }

    private void setContentItem(RemindBean data, Holder holder) {
        int type = data.type;
        String title = null;
        int color = 0;
        switch (type) {
            case RemindBean.TYPE_CUSTOMER:
                Custom custom = (Custom) data.data;
                title = custom.name + "的生日";
                holder.type.setText("生日");
                holder.type.setBackgroundResource(R.drawable.shape_remind_type_customer_bg);
                setCustomerBirthDay(holder.date, custom);
                color = mCustomerColor;
                break;

            case RemindBean.TYPE_HOLIDAY:
                Holiday holiday = (Holiday) data.data;
                title = holiday.getName();
                holder.type.setText("节日");
                holder.type.setBackgroundResource(R.drawable.shape_remind_type_holiday_bg);
                holder.date.setText(DateUtils.formatYYYYMMDD(holiday.getDate()));
                holder.date.setCompoundDrawables(null, null, null, null);
                color = mHolidayColor;
                break;

            case RemindBean.TYPE_INSURE:
                Insurance bean = (Insurance) data.data;
                title = "客户保单快到期啦，快去续保吧";
                holder.type.setText("续保");
                holder.type.setBackgroundResource(R.drawable.shape_remind_type_insurance_bg);
                holder.date.setText("客户：" + bean.getCustomer_name() + '\n'
                        + "保单：" + bean.getName() + '\n'
                        + "到期日：" + DateUtils.formatYYYYMMDD(bean.getRenew_date()));
                holder.date.setCompoundDrawables(null, null, null, null);
                color = mInsuranceColor;
                break;
        }
        setTextColor(holder, color);
        holder.title.setText(title);
        if (data.difference == 0) {
            holder.countDown.setText(type == RemindBean.TYPE_HOLIDAY ? "进行中" : "今天");
            holder.countDownUine.setText("");
        } else {
            holder.countDown.setText(String.valueOf(data.difference));
            holder.countDownUine.setText("天");
        }
    }

    private void setTitleItem(RemindBean data, TitleHoler holder) {
        holder.title.setText((String) data.data);
    }

    private void setCustomerBirthDay(TextView date, Custom custom) {
        int birtyType = custom.birthdayType;
        if (birtyType == DatePickerDialog.OnDateSetListener.TYPE_GREGORIAN_NO_YEAR
                || birtyType == DatePickerDialog.OnDateSetListener.TYPE_GREGORIAN_YEAR) {//无年份公历  0
            date.setText(DateUtils.format(Long.valueOf(custom.birthday)));
            date.setCompoundDrawables(null, null, null, null);
        } else if (birtyType == DatePickerDialog.OnDateSetListener.TYPE_LUNAR_NO_YEAR
                || birtyType == DatePickerDialog.OnDateSetListener.TYPE_LUNAR_YEAR) {//无年份农历   2
            date.setText(DateUtils.gregorianToLunar(Long.valueOf(custom.birthday)));
            date.setCompoundDrawables(mLunarMarkIcon, null, null, null);
        }
    }

    private void setTextColor(Holder holder, int color) {
        holder.type.setTextColor(color);
        holder.countDown.setTextColor(color);
        holder.countDownUine.setTextColor(color);
    }

    private String getInsuranceTitle(Insurance bean) {
        String insuranceName = bean.getName();
        if (StringUtils.isEmpty(insuranceName)) {
            insuranceName = mContext.getString(R.string.custom_insure_default_name, DateUtils.formatYYYYMMDD(bean.getEffect_date()));
        }
        return new StringBuffer().append("保险费到期日(")
                .append(bean.getCustomer_name())
                .append("-")
                .append(insuranceName)
                .append(")").toString();
    }

    class Holder {
        public TextView type;
        public TextView title;
        public TextView date;
        public TextView countDown;
        public TextView countDownUine;
    }

    class TitleHoler {
        public TextView title;
    }
}
